package com.rjgc.title.controller.apply;

import com.rjgc.title.common.result.Result;
import com.rjgc.title.model.entity.apply.DisciplineYoungCurriculum;
import com.rjgc.title.model.entity.apply.EducationProject;
import com.rjgc.title.service.EducationProjectService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @program: title-parent
 * @author: Mr.Zhuo
 * @create: 2022-04-25 23:58
 **/
@Api(tags = "教育教学项目(课题)  ")
@RestController
@RequestMapping("/api/education")
public class EducationProjectController {

    @Autowired
    private EducationProjectService educationProjectService;

    /**
     * 保存用户
     * @param request
     * @param educationProject
     * @return
     */
    @PostMapping("inner/save")
    public Result save(HttpServletRequest request, @RequestBody EducationProject educationProject) {
        // 获取当前对象的id
        String userId = (String) request.getSession().getAttribute("userId");
        educationProject.setUserId(Long.parseLong(userId));
        // 调用service方法进行保存
        educationProjectService.save(educationProject);
        return Result.ok();
    }

    /**
     * 根据用户id查询所有
     *
     * @param request
     * @return
     */
    @GetMapping("inner/getAll")
    public Result getAll(HttpServletRequest request) {
        // 获取当前对象的id
        String userId = (String) request.getSession().getAttribute("userId");
        List<EducationProject> educationProjects  = educationProjectService.getAll(userId);

        return Result.ok(educationProjects);
    }


    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("inner/getById/{id}")
    public Result getById(@PathVariable("id") Long id) {
        // 调用service方法进行查询
        EducationProject educationProject = educationProjectService.getById(id);

        // 返回数据
        return Result.ok(educationProject);
    }

    /**
     *  根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("inner/deleteById/{id}")
    public Result deleteById(@PathVariable("id") Long id) {
        // 调用service方法进行查询
        educationProjectService.removeById(id);

        return Result.ok();
    }

    /**
     * 更新操作
     * @param educationProject
     * @return
     */
    @PostMapping("inner/updateById")
    public Result updateById(@RequestBody EducationProject educationProject) {
        // 调用service方法进行更新
        educationProjectService.updateById(educationProject);

        return Result.ok();
    }
}
